﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onGetIndex(int index)
{
    ui->listWidget->addItem("");
    QString path = QString(":/imgs/%1.png").arg(index);
    ui->listWidget->item(ui->listWidget->count() - 1)->setIcon(QIcon(path));
    ui->listWidget->item(ui->listWidget->count() - 1)->setToolTip(QString::number(index));

    if(index == 12){

        ui->listWidget->item(ui->listWidget->count() - 1)->setToolTip("你得到了一张纸条,上面写着:我记得我把照片放在了书架上.");
        QMessageBox::information(0,"提示","你得到了一张纸条,上面写着:我记得我把照片放在了书架上.");

        QGraphicsRectItem* img13 = new QGraphicsRectItem();
        img13->setZValue(1);
        img13->setRect(286,36,100,220);


        img13->setData(0,"13");
        scene1.addItem(img13);

    }
    if(index == 13){
        QGraphicsPixmapItem* img11 = new QGraphicsPixmapItem();
        img11->setZValue(1);
        img11->setPixmap(QPixmap(":/imgs/1.png").scaled(30,30));
        img11->setPos(314,53);
        img11->setData(0,"1");
        scene1.addItem(img11);
    }
    if(index == 23){
        QGraphicsPixmapItem* img22 = new QGraphicsPixmapItem();
        img22->setZValue(1);
        img22->setPixmap(QPixmap(":/imgs/22.png").scaled(30,30));
        img22->setPos(580,380);
        img22->setData(0,"22");
        scene2.addItem(img22);
    }
    if(index == 22){
        QMessageBox::information(0,"提示","你得到了一把钥匙,似乎能打开某处的柜子.");
        ui->listWidget->item(ui->listWidget->count() - 1)->setToolTip("你得到了一把钥匙,似乎能打开某处的柜子.");
        m_key = true;
    }
    if(index == 34){
        if(m_key){
            QMessageBox::information(0,"提示","使用钥匙打开了柜子,获得了一张拼图.");
            onGetIndex(3);
        }else{
            QMessageBox::warning(0,"警告","柜子被锁住了,需要钥匙才能打开.");
            QGraphicsRectItem* img34 = new QGraphicsRectItem();
            img34->setZValue(1);
            img34->setRect(619,283,40,80);
            ;
            img34->setData(0,"34");
            scene3.addItem(img34);
        }
    }



}

void MainWindow::init()
{
    ui->listWidget->setIconSize(QSize(50,50));

    ui->stackedWidget->setCurrentWidget(ui->pag1);

    m_key = false;


    ui->view1->setScene(&scene1);
    ui->view2->setScene(&scene2);
    ui->view3->setScene(&scene3);
    ui->view4->setScene(&scene4);
    ui->view5->setScene(&scene5);

    QGraphicsPixmapItem* img1 = new QGraphicsPixmapItem();
    img1->setZValue(0);
    img1->setPixmap(QPixmap(":/imgs/img1.jpg"));
    scene1.addItem(img1);

    QGraphicsPixmapItem* img12 = new QGraphicsPixmapItem();
    img12->setZValue(1);
    img12->setPixmap(QPixmap(":/imgs/12.png").scaled(30,15));
    img12->setPos(539,335);
    img12->setData(0,"12");

    scene1.addItem(img12);


    QGraphicsPixmapItem* img2 = new QGraphicsPixmapItem();
    img2->setZValue(0);
    img2->setPixmap(QPixmap(":/imgs/img2.jpg"));
    scene2.addItem(img2);
    QGraphicsPixmapItem* img22 = new QGraphicsPixmapItem();
    img22->setZValue(1);
    img22->setPixmap(QPixmap(":/imgs/2.png").scaled(25,25));
    img22->setPos(597,254);
    img22->setData(0,"2");
    scene2.addItem(img22);
    QGraphicsRectItem* img23 = new QGraphicsRectItem();
    img23->setZValue(1);
    img23->setRect(523,325,50,50);
    img23->setData(0,"23");
    scene2.addItem(img23);

    QGraphicsPixmapItem* img3 = new QGraphicsPixmapItem();
    img3->setZValue(0);
    img3->setPixmap(QPixmap(":/imgs/img3.jpg"));
    scene3.addItem(img3);
    QGraphicsPixmapItem* img33 = new QGraphicsPixmapItem();
    img33->setZValue(1);
    img33->setPixmap(QPixmap(":/imgs/3.png").scaled(30,30));
    img33->setPos(596,253);
    img33->setData(0,"3");
    img33->hide();
    scene3.addItem(img33);
    QGraphicsRectItem* img34 = new QGraphicsRectItem();
    img34->setZValue(1);
    img34->setRect(619,283,40,80);
    img34->setData(0,"34");
    scene3.addItem(img34);

    QGraphicsPixmapItem* img4 = new QGraphicsPixmapItem();
    img4->setZValue(0);
    img4->setPixmap(QPixmap(":/imgs/img4.jpg"));
    scene4.addItem(img4);
    QGraphicsPixmapItem* img44 = new QGraphicsPixmapItem();
    img44->setZValue(1);
    img44->setPixmap(QPixmap(":/imgs/4.png").scaled(50,4));
    img44->setPos(284,397);
    img44->setData(0,"4");
    scene4.addItem(img44);

    QGraphicsPixmapItem* img5 = new QGraphicsPixmapItem();
    img5->setZValue(0);
    img5->setPixmap(QPixmap(":/imgs/img5.jpg"));
    scene5.addItem(img5);

    connect(ui->view1,SIGNAL(getPix(int)),this,SLOT(onGetIndex(int)));
    connect(ui->view2,SIGNAL(getPix(int)),this,SLOT(onGetIndex(int)));
    connect(ui->view3,SIGNAL(getPix(int)),this,SLOT(onGetIndex(int)));
    connect(ui->view4,SIGNAL(getPix(int)),this,SLOT(onGetIndex(int)));
    connect(ui->view5,SIGNAL(getPix(int)),this,SLOT(onGetIndex(int)));

}




void MainWindow::on_actScene1_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pag1);
}


void MainWindow::on_actScene2_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pag2);
}


void MainWindow::on_actScene3_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pag3);
}


void MainWindow::on_actScene4_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pag4);
}


void MainWindow::on_actScene5_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pag5);
}


void MainWindow::on_pushButton_clicked()
{
    if(ui->listWidget->count() == 0){
        QMessageBox::warning(0,"警告","还没有找到拼图碎片哦.");
    }else{
        Dialog d(ui->listWidget,this);
        d.exec();
    }


}

